package youcaihuaoa.weetop.com.ui.apply

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import com.robot.base.util.arouter.ARouterUtil
import youcaihuaoa.weetop.com.BR
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppActivityMyApplyListBinding
import youcaihuaoa.weetop.com.ui.adapter.ApplyAdapter
import youcaihuaoa.weetop.com.vm.apply.MyApplyListViewModel


@Route(path = AppConstants.Router.Main.A_MY_APPLY_LIST)
class MyApplyListActivity : GLBaseActivity<AppActivityMyApplyListBinding, MyApplyListViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_my_apply_list

    override fun initVariableId(): Int = BR.viewModel

    private lateinit var shopItemAdapter: ApplyAdapter

    override fun initComponents() {
        addSingleTitleBar("我的申请")
        shopItemAdapter = ApplyAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (shopItemAdapter.data[position].template_type == "99") {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, shopItemAdapter.data[position].zdy_form_id)
                            putString(AppConstants.BundleKey.ID, shopItemAdapter.data[position].id)
                            putString(
                                AppConstants.BundleKey.TITLE,
                                shopItemAdapter.data[position].public_title
                            )
                            putString(
                                AppConstants.BundleKey.TYPE,
                                shopItemAdapter.data[position].template_type
                            )
                            putInt(AppConstants.BundleKey.STATUS, 1)
                        })
                } else {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle().apply
                        {
                            putString(AppConstants.BundleKey.ID, shopItemAdapter.data[position].id)
                            putString(
                                AppConstants.BundleKey.TITLE,
                                shopItemAdapter.data[position].public_title
                            )
                            putString(
                                AppConstants.BundleKey.TYPE,
                                shopItemAdapter.data[position].template_type
                            )
                            putInt(AppConstants.BundleKey.STATUS, 1)
                        })
                }
            }
        }
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = shopItemAdapter
        }

        viewModel.examineUserData()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uiChangeEvent.dataEvent.observe(this) {
            it ?: return@observe
            shopItemAdapter.setList(it)
        }

        LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_APPLY).observe(this) {
            viewModel.examineUserData()
        }
    }
}